﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Text;

public partial class Pages_Public_JointVenture : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindDropdownLists();
            ShowSuccessMessage();
        }
    }

    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle("Joint Venture Request");
    }
    protected void ShowSuccessMessage()
    {
        if (String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.SUCCESS_MESSAGE), "True", false) == 0)
        {
            pnlFormContainer.Visible = false;
            pnlSuccessMessage.Visible = true;
        }
    }
    protected void BindDropdownLists()
    {
        BindDropdownList.Cities(ddlLCity, ConfigReader.SiteBusinessCountryID);
        BindDropdownList.Cities(ddlPreferredCityID, ConfigReader.SiteBusinessCountryID);
        for (int i = 1; i <= 100; i++)
        {
            ddlLRationOwner.Items.Add(new ListItem(i.ToString(), i.ToString()));
            ddlLRationDeveloper.Items.Add(new ListItem(i.ToString(), i.ToString()));
            ddlRatioOwner.Items.Add(new ListItem(i.ToString(), i.ToString()));
            ddlRatioDeveloper.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        ddlLRationOwner.SetSelectedItem("60");
        ddlLRationDeveloper.SetSelectedItem("40");
        ddlRatioOwner.SetSelectedItem("60");
        ddlRatioDeveloper.SetSelectedItem("40");
    }
    protected void PopulateObjectForDeveloper(JointVenture venture)
    {
        venture.Created = DateTime.Now;
        venture.CustomerName = txtCustomerName.Text;
        venture.Address = txtAddress.Text;
        venture.Phone = txtPhone.Text;
        venture.Fax = txtFax.Text;
        venture.Email = txtEmail.Text;
        venture.PreferredCityID = ddlPreferredCityID.SelectedValue.ToInt();
        venture.PreferredLocation = txtPreferredLocation.Text;
        venture.LandSize = txtLandSize.Text;
        venture.PreferredRatioOwner = ddlRatioOwner.SelectedValue.ToInt();
        venture.PreferredRatioDeveloper = ddlRatioDeveloper.SelectedValue.ToInt();
        venture.PropertyDescription = txtPropertyDescription.Text;        
        venture.VentureType = 2;
    }

    protected void PopulateObjectForLandOwner(JointVenture venture)
    {
        venture.Created = DateTime.Now;
        venture.CustomerName = txtLName.Text;
        venture.Address = txtLAddress.Text;
        venture.Phone = txtLPhone.Text;
        venture.Fax = txtLFax.Text;
        venture.Email = txtLEmail.Text;
        venture.PreferredCityID = ddlLCity.SelectedValue.ToInt();
        venture.PreferredLocation = txtLPreferredLocation.Text;
        venture.LandSize = txtLLandSize.Text;
        venture.PreferredRatioOwner = ddlLRationOwner.SelectedValue.ToInt();
        venture.PreferredRatioDeveloper = ddlLRationDeveloper.SelectedValue.ToInt();
        venture.PropertyDescription = txtLPropertyDescription.Text;
        venture.VentureType = 1;        
    }
    protected void SendNotificationToAdmin(JointVenture venture)
    {
        StringBuilder template = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
        if (template.Length > 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Dear Admin,<br/>");
            sb.AppendFormat("A new Joint Venture Request has been submitted, and waiting for your review. Click on the link below to See the details.<br /><br />");
            sb.AppendFormat("<a href='{0}{1}?{2}={3}'>{0}{1}?{2}={3}</a><br /><br />", WebUtil.GetDomainAddress(),
                AppConstants.Pages.JOINT_VENTURE_DETAILS, AppConstants.QueryString.ID, venture.ID);
            //sb.AppendFormat("<b>Note:</b> If you cannot click on the link then Copy it and Paste to your browser's address bar to make your account active.");
            ///Proejct Details
            sb.Append("<br /><br />");
            //sb.AppendFormat("<b><u>Project Details:</u></b><br />");
            //sb.AppendFormat("<b>Company Name:</b> {0}<br />", project.Customer.Name.HtmlEncode());
            //sb.AppendFormat("<b>Name:</b> {0}<br />", project.Name.HtmlEncode());
            //sb.AppendFormat("<b>Description:</b> {0}<br />", WebUtil.FormatText(project.Description));
            //sb.AppendFormat("<b>Project Type:</b> {0}<br />", project.PropertyType.Name.HtmlEncode());

            template.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());
            template.Replace(AppConstants.ETConstants.MESSAGE, sb.ToString());
            String fromEmail = ConfigReader.SupportEmail;
            String toEmail = ConfigReader.AdminEmail;
            String subject = String.Format("New Joint Venture Request.");
            WebUtil.SendMail(fromEmail, toEmail, String.Empty, String.Empty, subject, template.ToString());
        }
    }
    protected void SaveJointVenture()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        JointVenture venture = new JointVenture();
        if (rdbLandOwner.Checked)
            PopulateObjectForLandOwner(venture);
        else
            PopulateObjectForDeveloper(venture);

        context.AddToJointVentures(venture);
        context.SaveChanges();

        SendNotificationToAdmin(venture);

        Response.Redirect(String.Format("{0}?{1}=True", Request.Url.AbsolutePath, AppConstants.QueryString.SUCCESS_MESSAGE));
    }  
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveJointVenture();            
            return;
        }
    }
}